package Exception;

import java.util.List;

public class Exception4 {
    public static void main(String[] args) {
        //多异常分别处理
        try
        {
            int[] arr = {1,2,3};
            System.out.println(arr[3]);
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println(e);
        }

        try
        {
            List<Integer> list = List.of(1,2,3);
            System.out.println(list.get(3));
        }
        catch(IndexOutOfBoundsException e)
        {
            System.out.println(e);
        }
        //java.lang.ArrayIndexOutOfBoundsException: 3
        //java.lang.IndexOutOfBoundsException: Index 3 out-of-bounds for length 3
        System.out.println("===================");
        //多异常一次捕获，多次处理
        try{
            int[] arr = {1,2,3};
            System.out.println(arr[3]);
            List<Integer> list = List.of(1,2,3);
            System.out.println(list.get(3));
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println(e);
        }
        catch(IndexOutOfBoundsException e)
        {
            System.out.println(e);
        }
        //java.lang.ArrayIndexOutOfBoundsException: 3
        //因为处理了第一个异常，第二个直接被跳过了
        System.out.println("===================");
        //多个异常一次处理
        try{
            int[] arr = {1,2,3};
            System.out.println(arr[3]);
            List<Integer> list = List.of(1,2,3);
            System.out.println(list.get(3));
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println(e);
        }
        System.out.println("后续代码");
    }
}
